Integrating Unity with Eclipse
Please not that every care has been taken to make this guide as friendly as possible to new users. However it will be a lot simpler to follow if you are at least familiar with using eclipse, the java programming language and have some knowledge of the android sdk.
To be able to use unity android with eclipse you will first need to have Eclipse configured for use with Android. There is great guide on how to do this in the android developer documentation.
Unity as a Library
Once you have eclipse installed and up to date you have to follow a few steps to get your unity project set up as a library in eclipse.
- Create a new empty unity project and build it for android. It does not matter at this point if the unity project contains any code or other assets. This part of the process is in no way future proof and will be subject to change with future release of unity android.
- Navigate to your new project folder and then into the Temp folder. In there is a folder called staging area.
- Copy the staging area folder and place it somewhere in your file system that you can use as a holding area. This holding area should not be in your eclipse workspace as it will lead to errors.
- Open Eclipse and create a new Android Project using File --> New --> Project.
- Select Android Project then click next.
- This will be the library part of your project so name the project accordingly to avoid any confusion.
- You will also want to select the option to create project from an existing source. This source being the files you copied to the folder you are using as a holding area. If you select the Staging Area folder then click OK you will see that eclipse has filled in the relevant fields for you.
- click finish on the project creation wizard.
- Finally right click the root folder in the Package explorer tab in Eclipse and select the Properties option then the android tab. Check the "Is Library" check box, click Apply and then OK.
This completes the steps that you need to carry out to have your unity project addressed as a library in eclipse.
Java Project
The next step is to create the eclipse project that will form the executable part of the project.
- Create another new android project.
- Name the project and select the Build Target and Min SDK Version to be the same as the Library project that you created.
- Make sure that the package name is the same as the bundle identifier in your unity project otherwise you can get problems with conflicting files in the generated java files in your project.
- Now the library you created needs to be added to the project, you do this by right clicking the JavaProject and selecting properties.
- You then need to Select Android and click add. A window should then popup select the library project that was created and click ok. Once you are back in the main window click apply.
- Now select Java Build Path from the list on the left and click add external jar. A window will now popup you need to navigate to your unity install directory and in Unity\Editor\Data\PlaybackEngines\androidplayer\bin there will be a file called classes.jar. Double click that and then click on ok.
This sets up a basic Java Project that is now linked to your unity project and the relevant libraries that we will later need.
Moving the Projects Assets
This is the important part to get your unity content into the Java Project so that it will display when you later build and run your Java Project. You now need to move all the files in the assets folder in the library project into the new project. This is because Android does not allow assets to be used in libraries. The best way to do this is from within the Eclipse IDE. If you expand the assets files within both parts of the package explorer and simply drag the bin and libs folders from the library part of your project to the other. This last step needs to repeated everytime your unity project is rebuilt or maybe you could write a script to handle moving the files everytime you build the project automatically. This could also be achieved using symlinks or junctions. At this point when you run the from the eclipse ide you will be asked which type of project that you want to run. Select android and click ok. After eclipse builds the project it will deploy it to your connected android device and you should see a hello world style application running.
Getting Your Unity Content Running
To get your unity project you need to make a few changes to the java source code. In your eclipse project there will be one source file (.java) open this file. The contents of this file will resemble something like this.
package com.Unity3D.EclipseIntegration; import android.app.Activity; import android.os.Bundle; public class EclipseIntegration extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
We need to change this. The first thing is to change the base class of the object. It should now instead extend UnityPlayerActivity. You will notice that when you do this that it is red underlined proclaiming errors. This is simply missing import directives in the file. The eclipse shortcut to auto resolve this is ctrl+shift+o. The fiel should now have the import statement.
import com.unity3d.player.UnityPlayerActivity;
If you run in this state you will see that you now have a full screen hello world application. This is not what you want we need one more change to get your unity content running.
In the onCreate function there is one line that is overriding the unity view in the android application. If you delete the line.
setContentView(R.layout.main);
The complete file should now look like this.
package com.Unity3D.EclipseIntegration; import android.os.Bundle; import com.unity3d.player.UnityPlayerActivity; public class EclipseIntegration extends UnityPlayerActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
If you run it now you will see your unity content running on your android device.
Page last updated: 2011-02-20